//
//  AddBuyCarViewController.m
//  LoveTrail
//
//  Created by giantumbrella on 15/6/10.
//  Copyright (c) 2015年 赵娟. All rights reserved.
//

#import "AddBuyCarViewController.h"
#import "CommonMacro.h"
#import "SubmitOrderVC.h"
#import "MyCarButtomView.h"
#import "CarBuyThingCell.h"
#import "ProductImgOtherModel.h"
#import "UIImageView+WebCache.h"
static int num = 1;
static int sum = 0;
@interface AddBuyCarViewController ()<UITableViewDataSource,UITableViewDelegate,accountButtonDelegate,reductionButtonDelegate>
{
    MyCarButtomView *myCarButtonView;
    
}

@end

@implementation AddBuyCarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.contentArr = [[NSMutableArray alloc] init];
    //获取数据源
    [self getAllData];
    self.title = @"购物车";
    self.view.backgroundColor = UIColorWithRGBA(241, 241, 241, 1);
    self.navigationItem.rightBarButtonItem = nil;
    
    //创建tableView
    [self createTableViewAndView];
 
}

#pragma mark 服务器数据  上界面传来数据
- (void)getAllData
{
        ProductImgOtherModel *model = [[ProductImgOtherModel alloc] init];
        [model decodeDic:self.pathArray[0]];
        [self.contentArr addObject:model];
    
}

#pragma mark tableview
- (void)createTableViewAndView
{
    CGFloat tableViewX = 0;
    CGFloat tableViewY = 0;
    CGFloat tableViewW = SCREEN_WIDTH;
    CGFloat tableViewH = SCREEN_HEIGHT*0.9;
    //创建tableview
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(tableViewX, tableViewY,tableViewW ,tableViewH)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = UIColorWithRGBA(241, 241, 241, 1);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //创建view
    myCarButtonView = [[MyCarButtomView alloc] initWithFrame:CGRectMake(tableViewX+5, tableViewY+tableViewH, tableViewW, SCREEN_HEIGHT-tableViewH)];
    myCarButtonView.delegate = self;
    [self.view addSubview:myCarButtonView];
}

#pragma mark UITableView dateSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.contentArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"Cell";
    CarBuyThingCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell==nil) {
        cell = [[CarBuyThingCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    ProductImgOtherModel *model = self.contentArr[indexPath.row];
    [cell.productImageView setImageWithURL:[NSURL URLWithString:model.imageURL]];
    cell.detailLabel.text = model.title;
    cell.detailLabel.numberOfLines = 0;
    [cell.detailLabel sizeToFit];
    cell.priceLabel.text = model.price;
    cell.numLabel.text = [NSString stringWithFormat:@"%d",num];
    cell.backgroundColor = UIColorWithRGBA(241, 241, 241, 1);
    cell.delegate = self;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 170;
}

//单元格返回的编辑风格，包括删除 添加 和 默认  和不可编辑三种风格
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle==UITableViewCellEditingStyleDelete) {
        //        获取选中删除行索引值
        NSInteger row = [indexPath row];
        //        通过获取的索引值删除数组中的值
        [self.contentArr removeObjectAtIndex:row];
        //        删除单元格的某一行时，在用动画效果实现删除过程
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

#pragma mark  购物车 商品数量加减
- (void)reductionAddButton:(UIButton *)button
{
    if (button.tag==1000) {
        if (num>0) {
            num--;
        }
        else
        {
            num=0;
        }
    }
    else
    {
        num++;
    }
    [self.tableView reloadData];
}

#pragma mark  accountButtonDelegate
- (void)accountButton
{
    SubmitOrderVC *submitOrder = [[SubmitOrderVC alloc] init];
    UINavigationController *submitOrderNav = [[UINavigationController alloc] initWithRootViewController:submitOrder];
    [self presentViewController:submitOrderNav animated:YES completion:nil];
}

@end
